using UnityEngine;

namespace ZFramework.Runtime
{
    public class ItemLogic : MonoBehaviour
    {
        private bool _available = false;
        private bool _visible = false;
        private Item _item = null;
        private Transform _cachedTransform = null;
        private int _originalLayer = 0;
        private Transform _originalTransform = null;

        public Item Item
        {
            get { return _item; }
        }

        public string Name
        {
            get { return gameObject.name; }


            set { gameObject.name = value; }
        }

        public bool Available
        {
            get { return _available; }
        }

        /// <summary>
        /// 获取或设置实体是否可见。
        /// </summary>
        public bool Visible
        {
            get { return _available && _visible; }
            set
            {
                if (!_available)
                {
                    Log.Warning("Item '{0}' is not available.", Name);
                    return;
                }

                if (_visible == value)
                {
                    return;
                }

                _visible = value;
                InternalSetVisible(value);
            }
        }

        /// <summary>
        /// 获取已缓存的 Transform。
        /// </summary>
        public Transform CachedTransform
        {
            get { return _cachedTransform; }
        }

        /// <summary>
        /// 实体初始化。
        /// </summary>
        /// <param name="userData">用户自定义数据。</param>
        protected internal virtual void OnInit(object userData)
        {
            if (_cachedTransform == null)
            {
                _cachedTransform = transform;
            }

            _item = GetComponent<Item>();
            _originalLayer = gameObject.layer;
            _originalTransform = CachedTransform.parent;
        }

        /// <summary>
        /// 实体回收。
        /// </summary>
        protected internal virtual void OnRecycle()
        {
        }

        /// <summary>
        /// 实体显示。
        /// </summary>
        /// <param name="userData">用户自定义数据。</param>
        protected internal virtual void OnShow(object userData)
        {
            _available = true;
            Visible = true;
        }

        /// <summary>
        /// 实体隐藏。
        /// </summary>
        /// <param name="isShutdown">是否是关闭实体管理器时触发。</param>
        /// <param name="userData">用户自定义数据。</param>
        protected internal virtual void OnHide(object userData)
        {
            gameObject.SetLayerRecursively(_originalLayer);
            Visible = false;
            _available = false;

            CachedTransform.SetParent(_originalTransform);
        }

        /// <summary>
        /// 实体轮询。
        /// </summary>
        protected internal virtual void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
        }

        /// <summary>
        /// 设置实体的可见性。
        /// </summary>
        /// <param name="visible">实体的可见性。</param>
        protected virtual void InternalSetVisible(bool visible)
        {
            gameObject.SetActive(visible);
        }
    }
}
